<script lang="ts" setup name="stockIncomeList">
import ProTable from "@/components/ProTable/index.vue";
import { ProTableInstance, ColumnProps } from "@/components/ProTable/interface";
import { getStockRealSheetList, deleteStockRealSheet } from "@/api/modules/inventory";
import { Inventory } from "@/api/interface";
import { ref, onBeforeUpdate } from "vue";
import { CirclePlus } from "@element-plus/icons-vue";
import { useRouter, useRoute } from "vue-router";
import { ElMessageBox, ElMessage } from "element-plus";

const router = useRouter();
const route = useRoute();
const proTable: ProTableInstance = ref();
const initParam = ref({ dateType: 1 });
const column: ColumnProps<Inventory.ResStockIncomeList> = [
  {
    label: "序号",
    type: "index",
    width: 60,
    search: {
      el: "date-picker",
      label: "单据日期",
      key: "date",
      order: 1,
      props: {
        type: "datetimerange",
        format: "YYYY-MM-DD HH:mm",
        valueFormat: "YYYY-MM-DD HH:mm"
      }
    }
  },
  {
    label: "条码",
    prop: "orderNo",
    isFilterEnum: false,
    search: {
      el: "input",
      key: "orderId"
    }
  },
  { label: "单据状态", prop: "checkFlagName" },
  { label: "损益单数量", prop: "quantity" },
  {
    prop: "amount",
    label: "损益金额",
    width: 100
  },
  {
    prop: "sysUserName",
    label: "制单人",
    width: 120
  },
  {
    prop: "createTime",
    label: "制单时间",
    width: 180
  },
  {
    prop: "checkerName",
    label: "审核人",
    width: 120
  },
  {
    prop: "checkTime",
    label: "审核时间",
    width: 180
  },
  {
    prop: "printStatusName",
    label: "打印状态",
    width: 100
  },
  { label: "操作", prop: "operation" }
];

const toCreate = (id: number) => {
  if (id !== 0) {
    router.push({
      path: "/inventory/stockIncomeEdit",
      query: {
        id: id
      }
    });
  } else {
    router.push(`/inventory/stockIncomeEdit`);
  }
};

const toDelete = async (id: number) => {
  await ElMessageBox.confirm("确定该损益订单吗删除吗?", "温馨提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning"
  });
  await deleteStockRealSheet({ id: id });
  ElMessage.success("删除成功");
  proTable.value?.getTableList();
};

onBeforeUpdate(() => {
  if (route.query.upList) {
    proTable.value?.getTableList();
  }
});
</script>

<template>
  <div class="table-box">
    <ProTable
      ref="proTable"
      :columns="column"
      :init-param="initParam"
      :request-api="getStockRealSheetList"
      pagination
      title="损益单列表"
      :indent="20"
      row-key="id"
    >
      <template #tableHeader>
        <el-button v-auth="'add'" :icon="CirclePlus" type="primary" @click="toCreate(0)">新增损益单</el-button>
      </template>

      <template #operation="{ row }">
        <el-button v-auth="'view'" link type="primary" @click="toCreate(row.id)">查看</el-button>
        <el-button v-auth="'edit'" link type="primary" @click="toCreate(row.id)">编辑</el-button>
        <el-button v-auth="'delete'" type="primary" link @click="toDelete(row.id)">删除</el-button>
      </template>
    </ProTable>
  </div>
</template>

<style lang="scss" scoped></style>
